/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.schema.fetcher;

import com.eng.scc.core.data.TruckModel;
import com.eng.scc.core.data.Vehicle;
import com.eng.scc.core.db.Database;
import com.eng.scc.core.db.Fetcher;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Rafael
 */
public class VehicleFetcher implements Fetcher<Vehicle>
{
    @Override
    public Vehicle fetch( ResultSet ps ) throws Exception
    {
        Vehicle v = new Vehicle();
        
        v.setId( ps.getInt( 1 ) );
        v.setDriverId( ps.getInt( 2 ) );
        v.setCompanyId( ps.getInt( 3 ) );
        v.setBrand( ps.getString( 4 ) );
        v.setModel( ps.getString( 5 ) );
        v.setPlate( ps.getString( 6 ) );
        v.setModelYear( ps.getInt( 7 ) );
        v.setManufacturingYear( ps.getInt( 8 ) );
        v.setColor( ps.getString( 9 ) );
        v.setChassi( ps.getString( 10 ) );
        v.setRenavam( ps.getString( 11 ) );
        v.setCapacity( ps.getDouble( 12 ) );
        v.setTruckModel( TruckModel.values()[ps.getInt( 13 )] );
        v.setAxles( ps.getInt( 14 ) );
        v.setMaxFuel( ps.getDouble( 15 ) );
        v.setManufacturingAvg( ps.getDouble( 16 ) );
        v.setInfo( ps.getString( 17 ) );
        v.setState( ps.getShort( 18 ) );
        
        return v;
    }

    @Override
    public void insert( Vehicle value, PreparedStatement ps ) throws Exception
    {
        value.setId( Database.getInstance().nextID( "core_vehicles" ) );
        
        ps.setInt( 1, value.getId() );
        ps.setInt( 2, value.getDriverId() );
        ps.setInt( 3, value.getCompanyId() );
        ps.setString( 4, value.getBrand() );
        ps.setString( 5, value.getModel() );
        ps.setString( 6, value.getPlate() );
        ps.setInt( 7, value.getModelYear() );
        ps.setInt( 8, value.getManufacturingYear() );
        ps.setString( 9, value.getColor() );
        ps.setString( 10, value.getChassi() );
        ps.setString( 11, value.getRenavam() );
        ps.setDouble( 12, value.getCapacity() );
        ps.setInt( 13, value.getTruckModel().getId() );
        ps.setInt( 14, value.getAxles() );
        ps.setDouble( 15, value.getMaxFuel() );
        ps.setDouble( 16, value.getManufacturingAvg() );
        ps.setString( 17, value.getInfo() );
        ps.setShort( 18, value.getState() );
    }

    @Override
    public void update( Vehicle value, PreparedStatement ps ) throws Exception
    {
        ps.setInt( 1, value.getDriverId() );
        ps.setInt( 2, value.getCompanyId() );
        ps.setString( 3, value.getBrand() );
        ps.setString( 4, value.getModel() );
        ps.setString( 5, value.getPlate() );
        ps.setInt( 6, value.getModelYear() );
        ps.setInt( 7, value.getManufacturingYear() );
        ps.setString( 8, value.getColor() );
        ps.setString( 9, value.getChassi() );
        ps.setString( 10, value.getRenavam() );
        ps.setDouble( 11, value.getCapacity() );
        ps.setInt( 12, value.getTruckModel().getId() );
        ps.setInt( 13, value.getAxles() );
        ps.setDouble( 14, value.getMaxFuel() );
        ps.setDouble( 15, value.getManufacturingAvg() );
        ps.setString( 16, value.getInfo() );
        ps.setShort( 17, value.getState() );
        ps.setInt( 18, value.getId() );
    }   
}
